package org.pwwong.fxscalautils.components.controls

import java.awt
import java.awt.Color._
import java.awt.Font
import java.awt.Font._

import com.sun.scenario.scenegraph.fx.FXText
import com.sun.scenario.scenegraph.SGAbstractShape
import com.sun.scenario.scenegraph.SGText.VAlign

import org.pwwong.fxscalautils.components.Component
import org.pwwong.fxscalautils.graphics._

import org.pwwong.scalautils.signals._
import org.pwwong.scalautils.signals.Predef._

object Text {
  def apply() = new Text
}

class Text extends Component {
  type FXImpl = FXText
  compNodeImpl = new FXText
  
  // Text
  val text = Property[String]("")
  val fillPaint = Property[awt.Paint](black)
  val border = Property[awt.Stroke](EmptyStroke())
  val borderPaint = Property[awt.Paint](ClearColor())
  val font = Property[Font](new Font(SANS_SERIF,BOLD,24))
  
  // Reactions
  text.onChange(this){ compNodeImpl.setText(_) }
  fillPaint.onChangeAndOnce(this){ compNodeImpl.setFillPaint(_)  }
  borderPaint.onChangeAndOnce(this){ compNodeImpl.setDrawPaint(_)  }
  border.onChangeAndOnce(this){ compNodeImpl.setDrawStroke(_) }
  
  // Must occur after compNodeImpl.setText
  override val preferredWidth:Signal[Double] = eval{ compNodeImpl.getBounds().getWidth } when(text.onChange,font.onChange)
  override val preferredHeight:Signal[Double] = eval{ compNodeImpl.getBounds().getHeight } when(text.onChange,font.onChange)
  
  // Constructor
  compNodeImpl.setVerticalTextAlignment(VAlign.TOP)
  compNodeImpl.setAntialiasingHint(awt.RenderingHints.VALUE_TEXT_ANTIALIAS_ON)
  compNodeImpl.setFont(font())
}
